package xyz.xtgl.merchant.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.core.util.CommonUtil;
import common.core.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import plus.logic.LogicBaseController;
import xyz.xtgl.merchant.entity.BMerchantUser;
import xyz.xtgl.merchant.entity.help.BMerchantUserHelp;
import xyz.xtgl.merchant.feign.BMerchantUserFeign;
import xyz.xtgl.merchant.service.IBMerchantUserService;

/**
 * <p>
 * b端用户表，用来存放商家用户信息，用户可以用登录名、邮箱、手机号码进行登录 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-02-14
 */

@Slf4j
@Controller
@RequestMapping("/merchant/b_merchant_user")
public class BMerchantUserController extends LogicBaseController<IBMerchantUserService, BMerchantUser> implements BMerchantUserFeign{

    @Autowired
    private IBMerchantUserService iBMerchantUserService;

    /**
     * 跳转的列表页面
     * @return 跳转页面
     */
    @RequestMapping("/listPage")
    public Object listPage() {
      return "bMerchantUser_list";
    }

    /**
     * 列表页面表格数据
     * @param query 查询条件
     * @param page 分页对象
     * @return page 分页对象
     */
    @ResponseBody
    @RequestMapping("/listData")
    public Result listData(QueryWrapper<BMerchantUser> query, Page<BMerchantUser> page) {
        return Result.success(iBMerchantUserService.page(page, query));
    }

    /**
     * 跳转到修改页面
     * @param id 唯一主键
     * @param model Spring提供的绑定器
     * @return 修改页面
     */
    @RequestMapping("/editPage")
    public Object editPage(String id, Model model) {
        if(CommonUtil.isNotEmpty(id)) {
            BMerchantUser entity = this.iBMerchantUserService.getById(id);
            if(CommonUtil.isNotEmpty(entity)) {
                model.addAttribute("entity",entity);
            }
        }
        return "bMerchantUser_edit";
    }

    /**
     * 数据保存
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/saveOrUpdateEntity")
    public Result saveOrUpdateEntity(BMerchantUser entity) {
        log.info("开始保存数据:{}" ,entity);
        this.iBMerchantUserService.saveOrUpdate(entity);
        return Result.success();
    }

    /**
     * 数据删除
     * @param queryWrapper 删除条件
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/removed")
    public Result removed(QueryWrapper<BMerchantUser> queryWrapper) {
        this.iBMerchantUserService.remove(queryWrapper);
        return Result.success();
    }

    @Override
    @ResponseBody
    public BMerchantUser loginByPhone(@RequestParam("phone") String phone) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq(BMerchantUserHelp.phone,phone);
        return this.iBMerchantUserService.getOne(queryWrapper);
    }
}
